package com.example.braden.answergame.component;

import android.content.Context;

import com.example.braden.answergame.base.BaseResult;
import com.example.braden.answergame.common.Constant;
import com.example.braden.answergame.common.LogUtils;
import com.example.braden.answergame.common.im.IMMessageListener;
import com.example.braden.answergame.common.im.IMUtil;
import com.example.braden.answergame.common.utils.ToastUtil;
import com.example.braden.answergame.common.view.AnswerCardLayout;
import com.example.braden.answergame.modules.main.domain.GameOverEntity;
import com.example.braden.answergame.modules.main.domain.PropMessageEntity;
import com.example.braden.answergame.modules.main.domain.QuestionEntity;
import com.example.braden.answergame.modules.main.domain.UserPropInfo;
import com.example.braden.answergame.modules.main.domain.UserStatusEntity;
import com.example.braden.answergame.modules.main.presenter.MainPresenter;

/**
 * Created by braden on 2018/3/6.
 * 答题卡逻辑管理
 */

public class CardDisplayManager implements IMMessageListener, AnswerCardLayout.ReadyOverCallback {

    private Context mContext;
    private CardDisplayLogicCallback logicCallback;
    private AnswerCardLayout cardLayout;
    private QuestionEntity questionEntity;
    private static UserStatusEntity userStatusEntity;
    private boolean isCanUseRevive;//是否可以使用复活卡
    private boolean isCanUseRemove;//是否可以使用排除卡
    private boolean isCanUseDelay;//是否可以使用延时卡
    private int currentPos;//当前问题的位置，正常是从1开始

    public CardDisplayManager(Context context, CardDisplayLogicCallback logicCallback,
                              AnswerCardLayout cardLayout) {
        this.mContext = context;
        this.logicCallback = logicCallback;
        this.cardLayout = cardLayout;
        this.cardLayout.setCardDisplayManager(this);
        this.cardLayout.setReadyOverCallback(this);
        IMUtil.getInstance().setMessageListener(this);
    }

    /**
     * 收到下发题目消息,开始倒计时，绑定题目和选项到UI，
     */
    @Override
    public void onReceiveMessage(QuestionEntity entity) {
        this.questionEntity = entity;
        /**
         * 每次新的一题，都要把userStatusEntity设置为null
         */
        //ToastUtil.showShort("收到问题");
        currentPos = entity.getBody().getQnum();
        clearUserStatusEntity();
        //如果准备倒计时已经结束则立刻显示问题，如果倒计时还没有结束，则等倒计时结束后，再显示问题
        //if (cardLayout.isReadyIsOver()) {
        sendQuesitonMsg();
        //}
    }
    /**
     * 展示问题
     */
    private void sendQuesitonMsg() {
        isCanUseRemove = true;
        isCanUseRevive = false;
        cardLayout.onReceiveQuestion(this.questionEntity);
        logicCallback.nextQuestion(questionEntity.getBody().getQnum());
    }

    public QuestionEntity getQuestionEntity() {
        return questionEntity;
    }

    /**
     * 收到下发答题状态消息，修改头像显示对错的图标，
     */
    @Override
    public void onReceiveAnswer(UserStatusEntity entity) {
        LogUtils.e("下发答案-onReceiveAnswer：" + cardLayout.getCountdownIsOver() + "--"
                + cardLayout.isOtherIsUseYanshi());
        //如果当前currentPos=0，则表示没有收到之前的问题消息，返回并提示用户已经答题失败。
        if (currentPos == 0) {
            //ToastUtil.showShort("你已经错过了答题时间");
            return;
        }
        userStatusEntity = entity;
        //如果答题倒计时结束，则直接显示答案(如果对方使用了延时卡，则不能立刻显示，需要等到延时结束)
        if (cardLayout.getCountdownIsOver()) {
            //对方使用了延时卡，且延时未结束，自己不能立马显示答案状态
            if (cardLayout.isOtherIsUseYanshi() && cardLayout.getOtherYanshiIsOver()) return;
            cardLayout.onReceiveAnswer(entity);
        }
    }

    public UserStatusEntity getUserStatusEntity() {
        return userStatusEntity;
    }

    /**
     * 答题倒计时结束
     */
    @Override
    public void countDownIsOver() {
        LogUtils.e(
                "下发答案-countDownIsOver：" + cardLayout.getCountdownIsOver() + "---isOtherIsUseYanshi:"
                        + cardLayout.isOtherIsUseYanshi());
        //如果自己使用了延时卡，且倒计时结束了
        if (cardLayout.isSelfIsUseYanshi()) {
            LogUtils.e("自己使用了延时卡：");
            if (userStatusEntity != null) {
                cardLayout.onReceiveAnswer(userStatusEntity);
            } else {
                LogUtils.e("userStatusEntity is null");
            }
        } else {//正常十秒倒计时结束
            //倒计时结束后，需要判断对方是否使用了延时卡，如果使用了延时卡这个时候userStatusEntity=null，
            if (cardLayout.isOtherIsUseYanshi()) {
                //更新自己的答题状态
                logicCallback.answerCountdownOver();
            } else {
                if (userStatusEntity != null) {
                    cardLayout.onReceiveAnswer(userStatusEntity);
                }
            }
        }
    }

    /**
     * 发新题时，置空UserStatusEntity
     */
    private void clearUserStatusEntity() {
        userStatusEntity = null;
    }

    /**
     * 收到下发使用道具消息
     */
    @Override
    public void onReceiveProp(PropMessageEntity entity) {
        logicCallback.onReceivePropMessage(entity);
    }

    /**
     * 收到下发答题结束消息
     */
    @Override
    public void onReceiveOver(GameOverEntity entity) {
        logicCallback.gameOver(entity);
    }

    /**
     * 收到下发的用户和道具信息
     */
    @Override
    public void onReceiverUser(UserPropInfo userPropInfo) {
        logicCallback.onReceiveUserProp(userPropInfo);
    }

    /**
     * 网络断开，
     */
    @Override
    public void onNetwokrDis() {
        logicCallback.networkDis();
    }

    /**
     * 用户答错
     * 自己答错后提示可以使用复活卡或者购买复活卡
     */
    public void answerWrong() {
        isCanUseRevive = true;
        logicCallback.answerWrong();
    }

    /**
     * 答题正确
     */
    public void answerRight() {
        logicCallback.answerRight();
    }

    public void showYanshiPopup() {
        logicCallback.showYanshiPopup();
    }

    public boolean getIsCanRevive() {
        return isCanUseRevive;
    }

    public boolean getIsCanRemove() {

        return isCanUseRemove;
    }

    public boolean isCanUseDelay() {
        return isCanUseDelay;
    }

    public void setCanUseDelay(boolean canUseDelay) {
        isCanUseDelay = canUseDelay;
    }

    public void setIsCanRevive(boolean isCanRevive) {
        this.isCanUseRevive = isCanRevive;
    }

    public void setIsCanRemove(boolean isCanRemove) {
        this.isCanUseRemove = isCanRemove;
    }

    public void propCountdownOver() {
        logicCallback.propOver();
    }

    /**
     * 上报答案
     *
     * @param qid      题目id
     * @param answerId 选择的选项id
     */
    public void uploadAnswer(int qid, int answerId) {
        logicCallback.uploadAnswer(qid, answerId);
    }

    public interface CardDisplayLogicCallback {
        void uploadAnswer(int qid, int answerId);

        void nextQuestion(int position);

        void answerWrong();

        void onReceivePropMessage(PropMessageEntity entity);

        /**
         * 道具倒计时结束，未使用道具
         */
        void propOver();

        /**
         * 游戏结束
         */
        void gameOver(GameOverEntity entity);

        /**
         * 收到下发的消息，答题正确
         */
        void answerRight();

        /**
         * 答题倒计时结束
         */
        void answerCountdownOver();

        /**
         * 显示延迟卡使用的提示
         */
        void showYanshiPopup();

        /**
         * 收到下发的用户信息和道具信息
         */
        void onReceiveUserProp(UserPropInfo userPropInfo);

        /**
         * 倒计时结束且没有收到消息
         */
        //void readyOverAndFail();

        /**
         * 网络断开，退出游戏
         */
        void networkDis();
    }
}
